<template>
	<view class="searchLayout">
		<view class="search">
			<uni-search-bar
			v-model.trim="queryParams.keyword"
			@confirm="onSearch"
			@cancel="onClear"
			@clear="onClear"
			focus
			placeholder="搜索"
			></uni-search-bar>
		</view>
		
		<view v-if="!classList.length || noSearch">
			<view v-if="historySearch.length" class="history">
				<view class="topTitle">
					<view class="text">最近搜索</view>
					<view class="icon" @click="removeHistory">
						<uni-icons type="trash" size="25"></uni-icons>
					</view>
				</view>
				<view class="tabs">
					<view
					v-for="tab in historySearch"
					:key="tab"
					@click="clickTab(tab)"
					class="tab"
					>{{tab}}</view>
				</view>
			</view>
			
			<view class="recommend">
				<view class="topTitle">
					<view class="text">热门搜索</view>
				</view>
				<view class="tabs">
					<view
					v-for="tab in recommendList"
					:key="tab"
					@click="clickTab(tab)"
					class="tab"
					>{{tab}}</view>
				</view>
			</view>
		</view>
		
		<view v-if="noSearch" class="noSearch">
			<uv-empty
			mode="search"
			icon="http://cdn.uviewui.com/uview/empty/search.png"
			></uv-empty>
		</view>
		
		<view v-else>
			<view class="list">
				<navigator
				v-for="item in classList"
				:key="item._id"
				:url="`/pages/preview/preview?id=${item._id}`"
				class="item"
				>
					<image :src="item.smallPicurl" mode="aspectFill"></image>
				</navigator>
			</view>
			<view v-if="noData || classList.length" class="loadingLayout">
				<uni-load-more :status="noData?'noMore':'loading'"></uni-load-more>
			</view>
		</view>
		
	</view>
</template>

<script setup>
import { ref } from 'vue'
import { onLoad, onUnload, onReachBottom } from "@dcloudio/uni-app"
import { apiSearchData } from '@/api/apis.js'

// 查询参数
const queryParams = ref({
	pageNum: 1,
	pageSize: 12,
	keyword: ''
})
// 搜索历史词
const historySearch = ref(uni.getStorageSync('historySearch') || [])
// 热门搜索词
const recommendList = ref(['宠物', '卡通', '帅哥', '美女'])
// 没有更多
const noData = ref(false)
// 没有搜索结果
const noSearch = ref(false)

// 搜索结果列表
const classList = ref([])

// 点击搜索
const onSearch = () => {
	// console.log(queryParams.value.keyword)
	// historySearch.value.unshift(queryParams.value.keyword)
	uni.showLoading()
	if (!queryParams.value.keyword.trim()) return
	historySearch.value = [...new Set([queryParams.value.keyword, ...historySearch.value])].slice(0, 10)
	uni.setStorageSync('historySearch', historySearch.value)
	initParams(queryParams.value.keyword)
	searchData()
	// queryParams.value.keyword = ''
}

// 点击清除
const onClear = () => {
	initParams()
}

// 点击标签进行搜索
const clickTab = (value) => {
	initParams(value)
	queryParams.value.keyword = value
	onSearch()
}

// 点击清空搜索记录
const removeHistory = () => {
	uni.showModal({
		title: '是否清空历史搜索？',
		success: (res) => {
			if (res.confirm) {
				// console.log('确认删除')
				uni.removeStorageSync('historySearch')
				historySearch.value = []
			}
		}
	})
}

const searchData = async () => {
	try {
		const res = await apiSearchData(queryParams.value)
		classList.value = [...classList.value, ...res.data]
		// 这个缓存可以点击预览再缓存，性能要好一些
		uni.setStorageSync('storageClassList', classList.value)
		// console.log(res.data.length, classList.value.length)
		if (queryParams.value.pageSize > res.data.length)
			noData.value = true
		if (res.data.length === 0 && classList.value.length === 0)
			noSearch.value = true
		// console.log(res)
		// console.log(noData.value, noSearch.value)
	} finally {
		uni.hideLoading()
	}
}

// 初始化数据
const initParams = (value='') => {
	classList.value = []
	noData.value = false
	noSearch.value = false
	queryParams.value = {
		pageNum: 1,
		pageSize: 12,
		keyword: value
	}
}

// 触底加载更多
onReachBottom(() => {
	if (noData.value) return
	queryParams.value.pageNum++
	searchData()
})

// 关闭页面
onUnload(() => {
	uni.removeStorageSync('storageClassList')
})
</script>

<style lang="scss" scoped>
.searchLayout {
	.search {
		padding: 0 10rpx;
	}
	.topTitle {
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 32rpx;
		color: #999;
	}
	.history {
		padding: 30rpx;
	}
	.recommend {
		padding: 30rpx;
	}
	.tabs {
		display: flex;
		align-items: center;
		flex-wrap: wrap;
		padding-top: 20rpx;
		.tab {
			margin-top: 20rpx;
			margin-right: 20rpx;
			padding: 10rpx 28rpx;
			font-size: 28rpx;
			color: #333;
			border-radius: 50rpx;
			background-color: #F4F4F4;
		}
	}
	.list {
		display: grid;
		grid-template-columns: repeat(3, 1fr);
		gap: 5rpx;
		padding: 20rpx 5rpx;
		.item {
			height: 440rpx;
			image {
				display: block;
				width: 100%;
				height: 100%;
			}
		}
	}
}
</style>
